package com.dingyh.shopwebbackground.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dingyh.shopwebbackground.entity.Commodity;
import com.dingyh.shopwebbackground.entity.ShopCar;
import com.dingyh.shopwebbackground.mapper.CommodityMapper;
import com.dingyh.shopwebbackground.mapper.ShopCarMapper;
import com.dingyh.shopwebbackground.service.ShopCarService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dingyuhan
 * @since 2023-02-27
 */
@Service
public class ShopCarServiceImpl extends ServiceImpl<ShopCarMapper, ShopCar> implements ShopCarService {

    @Autowired
    ShopCarMapper shopCarMapper;

    @Autowired
    CommodityMapper commodityMapper;

    @Override
    public List<ShopCar> getAllCar(String userid) {
        List<ShopCar> shopCars = shopCarMapper.getAllCar(userid);
        shopCars.forEach(shopCar -> {
            LambdaQueryWrapper<Commodity> lambdaQueryWrapper = new LambdaQueryWrapper();
            lambdaQueryWrapper.eq(Commodity::getId,shopCar.getCommodityId());
            Commodity commodity = commodityMapper.selectOne(lambdaQueryWrapper);
            shopCar.setPrice(commodity.getPrice());
            shopCar.setTitle(commodity.getTitle());
            shopCar.setImg(commodity.getImg());
            shopCar.setStock(commodity.getStock());
            shopCar.setCompanyname(commodity.getCompanyname());
        });
        return shopCars;
    }

    @Override
    public boolean removeWillBuy(String id) {
        try {
            return removeById(id);
        }catch (Exception e){
            System.out.println(e);
            return false;
        }
    }

    @Override
    public boolean updQty(ShopCar shopCar) {
        try {
            return updateById(shopCar);
        }catch (Exception e){
            System.out.println(e);
            return false;
        }
    }

    @Override
    public ShopCar ifExistGoods(String commodityId, String userId) {
        LambdaQueryWrapper<ShopCar> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(ShopCar::getCommodityId,commodityId);
        queryWrapper.eq(ShopCar::getUserId,userId);
        return baseMapper.selectOne(queryWrapper);
    }
}
